package com.tian.scroll.gesture.fragment;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewGroup;

import com.tian.scroll.R;
import com.tian.scroll.databinding.FragmentScaleGestureBinding;

/**
 * @author tiansongtao, Email:769001510@qq.com, created on 2021/1/19 15:07
 * <p>
 * 双手势识别
 */
public class ScaleGestureFragment extends Fragment {

    private static final String TAG = "ScaleGestureFragment";

    private FragmentScaleGestureBinding b;

    private ScaleGestureDetector gestureDetector;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        b = DataBindingUtil.inflate(inflater, R.layout.fragment_scale_gesture, container, false);
        initDoubleGesture();
        b.mbtScaleGesture.setOnTouchListener((v, event) -> gestureDetector.onTouchEvent(event));
        return b.getRoot();
    }

    private void initDoubleGesture() {
        gestureDetector = new ScaleGestureDetector(requireActivity(), new ScaleGestureDetector.SimpleOnScaleGestureListener() {

            @Override
            public boolean onScaleBegin(ScaleGestureDetector detector) {
                Log.i(TAG, "onScaleBegin: ");
                return super.onScaleBegin(detector);
            }

            @Override
            public boolean onScale(ScaleGestureDetector detector) {
                Log.i(TAG, "onScale: " + detector.getScaleFactor());
                b.mbtScaleGesture.setScaleX(detector.getScaleFactor());
                b.mbtScaleGesture.setScaleY(detector.getScaleFactor());
                return super.onScale(detector);
            }

            @Override
            public void onScaleEnd(ScaleGestureDetector detector) {
                Log.i(TAG, "onScaleEnd: ");
                super.onScaleEnd(detector);
            }
        });
    }
}